from django.shortcuts import render,HttpResponse
from django.utils.deprecation import MiddlewareMixin
from jilu.models import *
from django.db.models import F

# class MD1(MiddlewareMixin):
#     def process_request(self, request):
#         print("--MD1------process_request----", id(request))
#         Content.objects.filter(id=1).update(count=F("count")+1)
#
#     def process_view(self, request, view_func, view_args, view_kwargs):
#         print("--MD1------process_view-------", id(request))
#
#     def process_response(self, request, response):
#         print("--MD1-----process_response-----", id(request))
#         # response["ss"] = "1234"
#         return response
#
#     def process_exception(self, request, exception):
#         print("--MD1-----process_exception-----", id(request))
#         return HttpResponse(exception)

# 中间件
class MD2(MiddlewareMixin):
    def process_request(self, request):
        print("--MD1------process_request----", id(request))
        if "HTTP_X_FORWARDED_FOR" in request.META:  # 获取用户真实ip地址
            user_ip = request.META["HTTP_X_FORWARDED_FOR"]
        else:
            user_ip = request.META["REMOTE_ADDR"]
        print(user_ip)
        if user_ip != user_ip:
            Content.objects.filter(id=1).update(count=F("count") + 1)
    def process_view(self, request, view_func, view_args, view_kwargs):
        print("--MD1------process_view-------", id(request))

    def process_response(self, request, response):
        print("--MD1-----process_response-----", id(request))
        # response["ss"] = "1234"
        return response

    def process_exception(self, request, exception):
        print("--MD1-----process_exception-----", id(request))
        return HttpResponse(exception)